//
//  YBLoginInputZjcViewController.m
//  Nectar
//
//  Created by famer on 2023/11/7.
//  Copyright © 2023 asance. All rights reserved.
//

#import "YBLoginInputZjcViewController+LayoutConstraints.h"
#import "YBNextStepView.h"
#import "YBNavigationBar.h"

@interface YBLoginInputZjcViewController ()

@end

@implementation YBLoginInputZjcViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setCleanEdgesForExtendedLayout];

    [self.view addSubview:self.navigationBar];
    [self.view addSubview:self.textField];
    [self.textField addSubview:self.placeholderLabel];
    [self.view addSubview:self.nextStepView];
    [self setLayoutConstraints];
}

#pragma mark - Private Method
- (void)doLogin{
    [self.view endEditing:YES];
    
    [SVProgressHUD show];
    self.view.userInteractionEnabled = NO;
    NSMutableDictionary *params = [NSDictionary baseReqParams];
    [params setObject:self.email forKey:@"email"];
    [params setObject:self.password forKey:@"password"];
    [params setObject:self.textField.text forKey:@"zjc"];
        
    NSString *urlString = [NSString stringWithFormat:@"%@",YB_DO_LOGIN_URL];
    
    [[YBHttpRequestManager shareInstance] Post:urlString params:params success:^(NSObject *resultObject) {
        YBLog(@"SUCCESS: %@",resultObject);
        
        self.view.userInteractionEnabled = YES;
        [SVProgressHUD dismiss];
        
        //判断网络是否返回错误信息
        YBResponseModel *obj = [YBResponseModel initWithResponseObject:(NSDictionary *)resultObject];
        if([obj success]){
            NSLog(@"-----%@", obj.data);
            NSLog(@"登录成功，去首页");
            //去首页
        }
        
    } failure:^(NSError *error) {
        YBErrorModel *yerror = [YBErrorModel ErrorObjForNSError:error];
    }];
}

#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView {
    if(textView.text.length>0){
        self.placeholderLabel.hidden = YES;
        [self.nextStepView setNextStepButtonEnabled];
    }else{
        self.placeholderLabel.hidden = NO;
        [self.nextStepView setNextStepButtonUnabled];
    }
}

#pragma mark - Getter Setter
YBLabelGetter(placeholderLabel, _placeholderLabel, [UIFont systemFontOfSize:12], @"Please enter the mnemonic words in order, separated by spaces.", [UIColor hexColor:@"9AA0A5"], NSTextAlignmentLeft);

- (YBNavigationBar *)navigationBar{
    if(!_navigationBar){
        _navigationBar = [[YBNavigationBar alloc] initWithFrame:CGRectZero];
        [_navigationBar setTitle:kLogin titleColor:[UIColor blackColor]];
        [_navigationBar setRightBarTitle:@"" forState:UIControlStateNormal];
        [_navigationBar setRightBarTitleColor:[UIColor hexColor:@"0099ff"] forState:UIControlStateNormal];
        __weak __typeof(&*self)weakSelf = self;
        _navigationBar.onLeftBarButtonBlock = ^(){
            __strong __typeof(&*weakSelf)strongSelf = weakSelf;
            if(!strongSelf) return;
            
            [strongSelf.navigationController dismissViewControllerAnimated:YES completion:nil];
        };
        _navigationBar.onRightBarButtonBlock = ^(){
            __strong __typeof(&*weakSelf)strongSelf = weakSelf;
            if(!strongSelf) return;
            [strongSelf.navigationController popViewControllerAnimated:YES];
        };
    }
    return _navigationBar;
}

- (UITextView *)textField{
    if(!_textField){
        _textField = [[UITextView alloc] initWithFrame:CGRectZero];
        _textField.returnKeyType = UIReturnKeyDone;
        _textField.textAlignment = NSTextAlignmentLeft;
        _textField.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);
        _textField.delegate = self;
        _textField.backgroundColor = [UIColor hexColor:@"FAFAFB"];
        _textField.font = [UIFont systemFontOfSize:[NSNumber adaptToHeight:12]];
        _textField.textColor = [UIColor hexColor:@"1E1E1E"];
    }
    return _textField;
}

- (YBNextStepView *)nextStepView{
    if(!_nextStepView){
        _nextStepView = [[YBNextStepView alloc] initWithFrame:CGRectZero];
        [_nextStepView setButtonTitle:kBtnNext];
        [_nextStepView setNextStepButtonUnabled];
        __weak __typeof(&*self)weakSelf = self;
        _nextStepView.onNextStepBlock = ^{
            __strong __typeof(&*weakSelf)strongSelf = weakSelf;
            if(!strongSelf) return;
            
            //去登录
            [strongSelf doLogin];
            
//            BOOL mobileLegal = [strongSelf checkMobileLegal];
            //            if(mobileLegal){
            //                YBLoginSmsViewController *loginSmsViewController = [[YBLoginSmsViewController alloc] init];
            //                loginSmsViewController.preMobile = strongSelf.accountInputView.textFieldValue;
            //                [strongSelf.navigationController pushViewController:loginSmsViewController animated:YES];
            //            }
            
//            BOOL passwordLegal = [strongSelf checkPasswordLegal];
//            if(mobileLegal && passwordLegal){
//                [strongSelf detectUserInfo];
//            }
        };
    }
    return _nextStepView;
}
@end
